package com.bjfu.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjfu.common_utils.R;
import com.bjfu.eduservice.entity.EduTeacher;
import com.bjfu.eduservice.entity.vo.TeacherQuery;
import com.bjfu.eduservice.service.EduTeacherService;
import com.bjfu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-29
 */
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
@Api(tags = {"教师管理"})
public class EduTeacherController {
    // service注入
    @Autowired
    private EduTeacherService eduTeacherService;
    // 1 查询讲师表中所有数据
    // rest风格
    @GetMapping("findAll")
    @ApiOperation(value = "教师列表")
    public R findAllTeacher() throws Exception {
        // 调用Service方法实现查询所有方法的操作
//        try{
//            int i = 0;
//            int i1 = 10 / i;
//        }catch (Exception e){
//            throw new GuliException(20001,"执行了自定义异常处理");
//        }
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "逻辑删除讲师")
    public R removeById(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable String id
    ){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("{current}/{limit}")
    @ApiOperation(value = "教师分页查询")
    public R findByPage(
            @ApiParam(name = "current",value = "当前页",required = true)
            @PathVariable(value = "current") Long current,

            @ApiParam(name = "limit",value = "页大小",required = true)
            @PathVariable(value = "limit") Long limit
    ){
        Page<EduTeacher> pageParam = new Page<>(current,limit);
        // 调用方法时候，底层封装，把分页所有数据封装到pageParam对象里面
        eduTeacherService.page(pageParam, null);
        long total = pageParam.getTotal();
        List<EduTeacher> records = pageParam.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }


    @PostMapping("condition/{current}/{limit}")
    @ApiOperation(value = "教师分页查询带条件")
    public R findByPageCondition(
        @ApiParam(name = "current",value = "当前页",required = true)
        @PathVariable Long current,
        @ApiParam(name = "limit",value = "页大小",required = true)
        @PathVariable Long limit,
        @ApiParam(name = "teacherQuery",value = "查询条件：<br>姓名<br>等级<br>注册时间",required = false)
        @RequestBody TeacherQuery teacherQuery
    ){
        // 创建Page对象
        Page<EduTeacher> pageParam = new Page<>(current,limit);
        eduTeacherService.pageQuery(pageParam,teacherQuery);

        //调用分页方法实现条件查询
        long total = pageParam.getTotal();
        List<EduTeacher> records = pageParam.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }


    @PostMapping("addTeacher")
    @ApiOperation(value = "添加讲师")
    public R addTeacher(
            @ApiParam(name = "teacher",value = "讲师对象",required = true)
            @RequestBody EduTeacher teacher
    ){
        boolean save = eduTeacherService.save(teacher);
        if(save){
            return R.ok();
        }else {
            throw new GuliException(20002,"添加讲师失败...");
        }
    }

    @ApiOperation(value = "查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(
            @ApiParam(name = "id",value = "讲师id", required = true)
            @PathVariable(name = "id") String id
    ){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @ApiOperation(value="更新讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher
    ) throws GuliException {
        boolean update = eduTeacherService.updateById(teacher);
        if(update){
            return R.ok();
        }else {
            throw new GuliException(20001,"更新讲师失败...");
        }

    }
}

